package com.developer.platform.application.command;

import cn.hutool.core.bean.BeanUtil;
import com.developer.platform.application.command.cmd.RoleAdd;
import com.developer.platform.application.command.cmd.RoleUpdate;
import com.developer.platform.domain.Role;
import com.developer.platform.domain.service.IRoleService;
import com.veeker.core.exceptions.BusinessException;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author ：qiaoliang
 * @date ：2020-11-09
 */
@Component
public class RoleCmdManager {

    private final IRoleService roleService;

    public RoleCmdManager(IRoleService roleService) {
        this.roleService = roleService;
    }

    /**
     * 新增角色
     *
     * @author ：qiaoliang
     * @param roleAdd : 新增信息
     * @date 2020-11-03 16:51
     */
    @Transactional(rollbackFor = Exception.class, noRollbackFor = BusinessException.class)
    public void save(RoleAdd roleAdd){
        Role role = new Role();
        BeanUtil.copyProperties(roleAdd, role);
        roleService.save(role, roleAdd.getIds());
    }
    /**
     * 修改角色
     *
     * @author ：qiaoliang
     * @param roleUpdate : 修改信息
     * @date 2020-11-03 16:51
     */
    @Transactional(rollbackFor = Exception.class, noRollbackFor = BusinessException.class)
    public void update(long id, RoleUpdate roleUpdate){
        Role role = new Role(id);
        BeanUtil.copyProperties(roleUpdate, role);
        roleService.update(role, roleUpdate.getIds());
    }

    /**
     * 删除角色
     *
     * @author ：qiaoliang
     * @param id : id
     * @date 2020-11-03 16:51
     */
    public void delete(long id){
        roleService.delete(id);
    }

}
